<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>

    <!--表单-->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="班级">
        <el-select v-model="searchForm.className" placeholder="班级" clearable>
          <el-option v-for="(className ,index) in $store.state.classList" :key="index" :label="className" :value="className"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="dialogFormVisible=true">添加</el-button>
      </el-form-item>
    </el-form>

    <!--班级列表-->
    <el-table :data="searchDatas" border style="width: 100%">
      <el-table-column  prop="className" label="班级" width="180"> </el-table-column>
      <el-table-column prop="createDate"  label="开班时间"></el-table-column>
      <el-table-column prop="classNum"  label="班级人数"></el-table-column>
      <el-table-column prop="endDate"  label="闭班时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain>编辑</el-button>
          <el-button size="mini" type="success" @click="completion(scope.$index)" v-show="scope.row.endDate===''||scope.row.endDate==null" plain>
            {{scope.$index}}闭班
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="40">
    </el-pagination>

    <!--添加层-->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="classForm">
        <el-form-item label="活动名称">
          <el-input v-model="classForm.className" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClass(),dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "Class",
    data() {
      return {
        searchForm: {},
        classForm:{},
        dialogFormVisible:false,
        classDatas:[
          {id:1,className:"一班",createDate:'2020-11-12',classNum:30,endDate:'2020-11-23'},
          {id:2,className:"二班",createDate:'2020-11-12',classNum:30,endDate:''},
          {id:3,className:"三班",createDate:'2020-11-12',classNum:30,endDate:''},
          {id:4,className:"四班",createDate:'2020-11-12',classNum:30,endDate:''},
        ],
        searchDatas:[]
      }
    },
    mounted() {
      this.searchDatas=this.classDatas
    },
    methods:{
      addClass(){
        if(this.classForm.className) {
          var date = new Date();
          this.classForm.createDate = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate()
          this.classForm.id = this.classDatas.length + 1
          this.classDatas.push(this.classForm)
          var className = this.classForm.className;
          this.$store.state.classList.push(className)
        }
      },
      completion(index){
        var date = new Date();
        this.classDatas[index].endDate=date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()
        this.classDatas.splice(index,1,this.classDatas[index])
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      },
      search(){
        var className = this.searchForm.className;
        if(className!='' && className!=null) {
          this.searchDatas = this.classDatas.filter((clas) => {
            return clas.className == className
          })
        }else {
          this.searchDatas=this.classDatas
        }
      }
    }
  }
</script>

<style scoped>
  .demo-form-inline {
    margin-top: 20px;
  }
</style>
